//===============================
//  功能 ：备注 
//  作者 ：上中野辅亚瑟王 
//  创建时间 ：2024-11-05  18-38-21 
//  Unity版本  ：2019.4.35f1 
//  变更时间 :  2024-11-05  18-38-21 
//===============================





using UnityEditor;
using System.IO;
using UnityEngine;


namespace ZS.U3D.EXPAND.ScriptTemplate
{
    
    public static class TemplateMenuItems
    {

        private static string templatePath = "Assets/ZS.U3D.EXPAND/ScriptTemplates/Editor/Templates/";

        [MenuItem("Assets/Create/C# Script From Template", priority = 81)]
        public static void MenuItem()
        {

            DirectoryInfo directoryInfo = new DirectoryInfo(templatePath);
            FileSystemInfo[] fileSystemInfos = directoryInfo.GetFileSystemInfos();

            TemplateWindow.Init(fileSystemInfos);

            //CreateScriptFromTemplateAsset("Class");
        }



        private class TemplateWindow : EditorWindow 
        {

            private static FileSystemInfo[] infos;

            public static void Init(FileSystemInfo[] fileSystemInfos)
            {
                infos = fileSystemInfos;
                TemplateWindow window = GetWindow(typeof(TemplateWindow)) as TemplateWindow;
                window.Show();
            }

            private static void CreateScriptFromTemplateAsset(string templateAssetName)
            {
                ProjectWindowUtil.CreateScriptAssetFromTemplateFile($"{templatePath}{templateAssetName}", $"New{templateAssetName}.cs");
            }

            private void OnGUI() 
            {
                EditorGUILayout.BeginVertical();
                {
                    foreach (FileSystemInfo file in infos)
                    {
                        int index = file.FullName.LastIndexOf('.');

                        string extension = file.FullName.Substring(index);

                        if (extension == ".meta")
                            continue;

                        if (GUILayout.Button($"{file.Name}"))
                        {
                            CreateScriptFromTemplateAsset(file.Name);

                            Close();
                        }
                        //Debug.Log(index + "  " + extension);
                    }



                    EditorGUILayout.EndVertical();
                };
            }
        }
    }
}